package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.constant.TransactionType;
import cn.ztuo.bitrade.dao.CesspitAwardDao;
import cn.ztuo.bitrade.entity.*;
import cn.ztuo.bitrade.entity.transform.AuthMember;
import cn.ztuo.bitrade.util.Md5;
import cn.ztuo.bitrade.util.PredicateUtils;
import cn.ztuo.bitrade.util.RedisUtil;
import com.alibaba.fastjson.JSONObject;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
public class CesspitAwardService {
    @Autowired
    private CesspitAwardDao cesspitAwardDao;
    @Autowired
    private MemberWalletService memberWalletService;
    @Autowired
    private CesspitAwardService awardService;
    @Autowired
    private MemberTransactionService transactionService;
    @Autowired
    private RedisUtil redisUtil;

    //挖矿收益币种
    private static String ORECOIN = "BIC";


    @Transactional(rollbackFor = Exception.class)
    public void obtain(AuthMember authMember, CesspitAward award) throws Exception {

        MemberWallet memberWallet = memberWalletService.findByCoinUnitAndMemberId(ORECOIN, authMember.getId());

        // 收益到账返还到资产
        RestTemplate restTemplate = new RestTemplate();
        Map<String, Object> postData = new HashMap<>();
        postData.put("mobile", authMember.getMobilePhone());
        postData.put("coin", memberWallet.getCoin().getUnit());
        postData.put("num", award.getNumber());
        postData.put("auth_code", Md5.md5String(authMember.getMobilePhone() + memberWallet.getCoin().getUnit() + "f84012c66608000aada33bfd2d8b7636"));

        JSONObject s = restTemplate.postForObject("http://www.bodhi.luxe/api/Docking/coin_zhuanzhang", postData, JSONObject.class);
        int status = s.getIntValue("status");

        if (status == 0) {
            throw new Exception("资产领取收益失败");
        }

        //修改已经发过奖励的记录收益状态
        awardService.updateStatus(award.getMobile());
        //创建收益记录
        MemberTransaction transaction = new MemberTransaction();
        transaction.setAmount(award.getNumber());
        transaction.setSymbol(memberWallet.getCoin().getName());
        transaction.setAddress("");
        transaction.setMemberId(memberWallet.getMemberId());
        transaction.setType(TransactionType.HASHRATE);
        transaction.setFee(BigDecimal.ZERO);
        transaction.setCreateTime(new Date());
        transactionService.save(transaction);
    }

    public CesspitAward save(CesspitAward awards) {
        return cesspitAwardDao.save(awards);
    }

    public List<CesspitAward> findAllList(Long type) {
        return cesspitAwardDao.findAllGroupBy(type);
    }

    public Page<CesspitAward> findAllListByMember(String mobile) {

        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();

        booleanExpressions.add(QCesspitAward.cesspitAward.type.loe(3L));

        booleanExpressions.add(QCesspitAward.cesspitAward.status.eq(1L));

        if (StringUtils.isNotEmpty(mobile)) {
            booleanExpressions.add(QCesspitAward.cesspitAward.mobile.eq(mobile));
        }

        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);

        //Sort sort = new Sort(Sort.Direction.ASC,"status");

        Pageable pageable = new PageRequest(0, 8);

        Page<CesspitAward> all = cesspitAwardDao.findAll(predicate, pageable);

        return all;
    }

    public CesspitAward findAllListTwo(Long type, String mobile) {
        return cesspitAwardDao.findAllGroupByTwo(type, mobile);
    }

    public int updateStatus(String mobile) {
        return cesspitAwardDao.updateStatus(mobile);
    }

    public BigDecimal findAllSum() {
        return cesspitAwardDao.findAllSum();
    }
}
